<?php

/**
 * 前台 地方首页 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: index.class.php 39 2011-07-11 17:12:35Z zwt007 $
 * http://www.t-mac.org；
 */
class difang extends Model
{

    private $article_table;
    private $link_table;
    private $users_table;
    private $category_table;
    private $add_category_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->article_table = '#@__article';
        $this->link_table = '#@__links';
        $this->users_table = '#@__users';
        $this->category_table = '#@__category';
        $this->add_category_table = '#@__addoncategory';
    }

    /**
     * 根据username取用户信息
     * @param type $username
     * @return type 
     */
    public function getUserinfo($username)
    {
        $sql = "SELECT uid, username, nicename, url, lianxi_img, qq_server FROM $this->users_table WHERE username = '$username' LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        return $rs;
    }

    /**
     * 获取资讯栏目
     * return article_class,pages
     */
    public function getCategoryArray($uid, $reid)
    {
        $sqltree = "SELECT a.cat_id, a.cat_name,a.category_nicename, a.urlfile, b.cat_name AS cat_name_add, a.cat_pid, b.cat_pid AS cat_pid_add FROM $this->category_table a LEFT JOIN $this->add_category_table b ON a.cat_id = b.cat_id AND b.uid ={$uid} WHERE (a.cat_pid = " . $reid . " OR b.cat_pid = " . $reid . ") AND (a.uid = '{$uid}' OR a.uid = 1) AND cat_type = 0 ORDER BY a.cat_order DESC, a.cat_id ASC";
        $rs = $this->db->getAll($sqltree);
        if (is_array($rs)) {
            foreach ($rs as $k => $v) {
                if ((!empty($v['cat_pid_add'])) && ($v['cat_pid_add'] != $reid)) {
                    continue;
                }
                $sqltree = "SELECT a.cat_id, a.cat_name, a.category_nicename, a.urlfile, b.cat_name AS cat_name_add, a.cat_pid, b.cat_pid AS cat_pid_add FROM $this->category_table a LEFT JOIN $this->add_category_table b ON a.cat_id = b.cat_id AND b.uid ={$uid} WHERE (a.cat_pid = " . $v['cat_id'] . " OR b.cat_pid = " . $v['cat_id'] . ") AND (a.uid = '{$uid}' OR a.uid = 1) AND a.cat_type = 0 ORDER BY a.cat_order DESC, a.cat_id ASC";
                $rs_two = $this->db->getAll($sqltree);
                $rss = array();
                if (is_array($rs_two)) {
                    foreach ($rs_two AS $kk => $vv) {
//                        echo $v['cat_id'];
//                        var_dump($vv);
//                        echo '<br><br>';
                        if ((!empty($vv['cat_pid_add'])) && ($vv['cat_pid_add'] != $v['cat_id'])) {
                            continue;
                        }
                        $cat_name = empty($vv['cat_name_add']) ? $vv['cat_name'] : $vv['cat_name_add'];
                        $rss[$vv['cat_id']]['cid'] = $vv['cat_id'];
                        $rss[$vv['cat_id']]['cat_name'] = $cat_name;
                        if (empty($vv['urlfile'])) {
                            $url_string_b = '';
                        } else {
                            $url_string_b = $vv['urlfile'];
                        }
                        $rss[$vv['cat_id']]['nicename'] = $url_string_b;
                    }
                }
                $result[$v['cat_id']]['cat_name'] = $v['cat_name'];
                $result[$v['cat_id']]['next'] = $rss;
                if (empty($v['urlfile'])) {
                    $url_string = $v['category_nicename'];
                } else {
                    $url_string = $v['urlfile'];
                }
                $result[$v['cat_id']]['nicename'] = $url_string;
            }
        }

        return $result; //最后返回树形菜单
    }

}